package com.heima.user.service.impl;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserApplicationService;
import com.heima.user.util.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.concurrent.TimeUnit;


//申请激活
@Service
public class ApUserApplicationServiceImpl implements ApUserApplicationService {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    ApUserMapper apUserMapper;

    @Override
    public ResponseResult application(String email) {
       if (email == null){
           CustException.cust(AppHttpCodeEnum.LOGIN_STATUS_ERROR);
       }

        ApUser apUser = apUserMapper.selectOne(new QueryWrapper<ApUser>().eq("email", email));
       if (apUser == null){
           CustException.cust(AppHttpCodeEnum.DATA_EXIST);
       }
        //生成uuid
        String uuid = UuidUtils.generateUuid();
        // redis 里面的key value
        redisTemplate.opsForValue().set(uuid,apUser.getId().toString());
        //设置email过期时间
        redisTemplate.expire(uuid,3, TimeUnit.MINUTES);
        //设置 邮箱  发邮件
        String mailContent = "<a href = 'http://localhost:9002/api/v1/activate/activateUser?uuid=" + uuid + "'>点击激活用户</a>";
        MailUtils.sendMail(email, mailContent,"账号激活申请");
        return  ResponseResult.okResult();
    }
}
